Queuing Simulation

We present in this notebook a simple bank customer arrival model.

We use a Poisson distribution to model the arrival and departure times.


In [9]:
import random

#define and initialize the parameters of the Poisson distributions
lambd_in = 0.5
lambd_out = 0.4

In [10]:
#bank variables
closing_time =  100      #initialize the bank closing time 
overtime = 0             #overtime the employees need to be paid for
    
#queue variables
num_arrivals = 0    #number of people in the que
num_departures = 0  #number of people who have been served
n = 0               #length of the queue  
max_line_length = 0 #the maximum length of the waiting line: 
    
#time variables
t = 0                                       #set the time of first arrival to 0
time_depart = float('inf')                  #set the first time of departure to infinity
time_arrive = random.expovariate(lambd_in)  #generate the first arrival

Next we initialize two arrays to keep track of the customers who arrive and who depart:


In [11]:
departures = []
arrivals = []

We simulate the arrivals and departures in a bank branch using a while loop


In [12]:
while t < closing_time or n >= 0:
        
        # case 1 - within business hours, a customer arrives before any customer leaves the queue 
        if time_arrive <= time_depart and time_arrive <= closing_time:
            
            t = time_arrive      # move time along to the time of the new arrival
            num_arrivals += 1    # increase the number of customers with the additional arrival
            n += 1               # we have an additional customer, increase the size of the waiting line by 1 
            
            # generate time of next arrival
            time_arrive = random.expovariate(lambd_in) + t
            
            #append the new customer to the arrival list
            arrivals.append(t)
            print("Arrival ", num_arrivals, "at time ", t)
            
            
            # generate time of departure 
            if n == 1:
                Y = random.expovariate(lambd_out)
                time_depart = t + Y
                
            ''' 
            print('Arrivals', arrivals)
            print('Departures', departures)
            '''       
                  
        # case 2 - within business hours, a customer departs before the next arrival
        elif time_depart < time_arrive and time_depart <= closing_time:
            
            # advance time to the next departure time
            t = time_depart
            
            # one more person served -> increase the count of clients who have been served
            num_departures += 1
            
            #update the departure list
            departures.append(t)
            print("Departure ", num_departures, "at time ", t)
            
            # one less person in line -> decrease the size of the waiting line
            n -= 1
            
            # if the queue is empt -> set the time of the next departure to infinity
            if n == 0:
                time_depart = float('inf')
                
            # if the queue isn't empty, generate the next time of departure
            else:
                Y = random.expovariate(lambd_out)
                time_depart = t + Y
            
            ''' 
            print('Arrivals', arrivals)
            print('Departures', departures)    
            ''' 
                  
        # case 3 - next arrival/departure happens after closing time and there are people still in the queue
        elif min(time_arrive, time_depart) > closing_time and n > 0:
            
            # advance time to next departure
            t = time_depart
            
            #update the departure list
            departures.append(t)
            
            #update the number of departures/clients served
            num_departures += 1     # one more person served
            
            print("Departure ", num_departures, "at time ", t)
            
            #update the queue
            n -= 1    # one less person in the waiting line
            
            
            # if line isn't empty, generate the time of the next departure
            if n > 0:
                Y = random.expovariate(lambd_out)
                time_depart = t + Y
            
            ''' 
            print('Arrivals', arrivals)
            print('Departures', departures) 
            ''' 
            
            
        # case 4 - next arrival/departure happens after closing time and there is nobody left in the queue
        elif min(time_arrive, time_depart) > closing_time and n == 0:
            
            # calculate overtime
            overtime = max(t - closing_time, 0)
            print('Overtime = ', overtime)
            
            '''
            print('Arrivals', arrivals)
            print('Departures', departures)
            '''
                      
            break


Arrival  1 at time  1.2830305693548143
Arrival  2 at time  1.7206365617426929
Arrival  3 at time  5.520045152648045
Departure  1 at time  7.864649511237831
Arrival  4 at time  7.910288858358761
Departure  2 at time  8.212414518885222
Departure  3 at time  8.606532896505152
Departure  4 at time  8.825945127686502
Arrival  5 at time  14.091247626620074
Departure  5 at time  18.69956811810046
Arrival  6 at time  22.01496561405647
Departure  6 at time  23.340074415868155
Arrival  7 at time  26.911365852145277
Arrival  8 at time  27.067467329432528
Arrival  9 at time  27.461907139779278
Arrival  10 at time  27.889029763874184
Departure  7 at time  30.280557331115308
Arrival  11 at time  31.30205663376647
Arrival  12 at time  31.330011366056116
Arrival  13 at time  31.745085683203765
Arrival  14 at time  31.920996045712336
Arrival  15 at time  33.737499053621946
Arrival  16 at time  34.26353535367393
Arrival  17 at time  37.123412430864846
Arrival  18 at time  39.65986145938473
Departure  8 at time  40.36311727874019
Arrival  19 at time  42.34722400209597
Departure  9 at time  43.76085406717446
Arrival  20 at time  44.103355422352564
Departure  10 at time  44.17258583562551
Departure  11 at time  45.206063418283165
Arrival  21 at time  45.40377930526148
Arrival  22 at time  47.367253810455004
Arrival  23 at time  48.76133665074659
Arrival  24 at time  50.265455195238765
Departure  12 at time  51.05723669510688
Arrival  25 at time  53.22631487664217
Departure  13 at time  53.79788852302882
Departure  14 at time  54.1049114611876
Arrival  26 at time  54.41308808519721
Departure  15 at time  55.30621616781526
Departure  16 at time  56.02212042304079
Arrival  27 at time  56.998194158340645
Arrival  28 at time  58.585446712292914
Arrival  29 at time  59.71537062252929
Departure  17 at time  59.973640470563986
Arrival  30 at time  61.13550812685068
Departure  18 at time  61.247789708759
Arrival  31 at time  61.91785628307149
Arrival  32 at time  62.45220021564855
Departure  19 at time  64.93180593037398
Departure  20 at time  65.48400980686225
Arrival  33 at time  65.88267897599259
Departure  21 at time  66.1370723471967
Arrival  34 at time  68.06004024478139
Arrival  35 at time  68.48920620544456
Arrival  36 at time  69.51843866679322
Departure  22 at time  74.42851234447934
Arrival  37 at time  75.10154033721976
Departure  23 at time  75.23714683990589
Arrival  38 at time  75.61667587804278
Departure  24 at time  76.4033512417975
Departure  25 at time  77.30158225251064
Departure  26 at time  81.93079989827848
Departure  27 at time  83.95007372511
Arrival  39 at time  85.1098783928812
Departure  28 at time  86.78350933100805
Arrival  40 at time  87.75831776229028
Departure  29 at time  88.94904986122758
Arrival  41 at time  90.66023190404631
Departure  30 at time  93.03267644428368
Arrival  42 at time  94.1286396916322
Arrival  43 at time  96.20447885343665
Arrival  44 at time  96.52208277805443
Departure  31 at time  96.93935834812083
Arrival  45 at time  97.29206534941872
Departure  32 at time  98.8119344895357
Arrival  46 at time  98.84816643443963
Departure  33 at time  105.75209457159715
113.18598522284027 13
Departure  34 at time  113.18598522284027
114.16413419916454 12
Departure  35 at time  114.16413419916454
116.62908919399902 11
Departure  36 at time  116.62908919399902
122.52021900285128 10
Departure  37 at time  122.52021900285128
126.6060043691084 9
Departure  38 at time  126.6060043691084
127.36801708652669 8
Departure  39 at time  127.36801708652669
129.33292641784686 7
Departure  40 at time  129.33292641784686
138.61562400131328 6
Departure  41 at time  138.61562400131328
146.7901765391917 5
Departure  42 at time  146.7901765391917
151.97332802429284 4
Departure  43 at time  151.97332802429284
156.599854146141 3
Departure  44 at time  156.599854146141
157.26061169117128 2
Departure  45 at time  157.26061169117128
158.0387268837508 1
Departure  46 at time  158.0387268837508
158.0387268837508 0
Overtime =  58.0387268837508

In [ ]: